package com.java110.mall.cart.cmd.storeOrder;

import com.alibaba.fastjson.JSONObject;
import com.java110.core.annotation.Java110Cmd;
import com.java110.core.context.ICmdDataFlowContext;
import com.java110.core.event.cmd.Cmd;
import com.java110.core.event.cmd.CmdEvent;
import com.java110.dto.storeOrder.OrderExpressDto;
import com.java110.dto.storeOrder.StoreOrderLogisticsDto;
import com.java110.intf.cart.IStoreOrderLogisticsV1InnerServiceSMO;
import com.java110.intf.chain.IExpressV1InnerServiceSMO;
import com.java110.utils.exception.CmdException;
import com.java110.utils.util.Assert;
import com.java110.utils.util.ListUtil;
import com.java110.vo.ResultVo;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

/**
 * 查询物流明细
 */
@Java110Cmd(serviceCode = "storeOrder.queryLogisticsDetail")
public class QueryLogisticsDetailCmd extends Cmd {

    @Autowired
    private IStoreOrderLogisticsV1InnerServiceSMO storeOrderLogisticsV1InnerServiceSMOImpl;

    @Autowired
    private IExpressV1InnerServiceSMO expressV1InnerServiceSMOImpl;

    @Override
    public void validate(CmdEvent event, ICmdDataFlowContext context, JSONObject reqJson) throws CmdException {
        Assert.hasKeyAndValue(reqJson, "orderId", "未包含订单编号");
    }

    @Override
    public void doCmd(CmdEvent event, ICmdDataFlowContext context, JSONObject reqJson) throws CmdException {

        StoreOrderLogisticsDto storeOrderLogisticsDto = new StoreOrderLogisticsDto();
        storeOrderLogisticsDto.setOrderId(reqJson.getString("orderId"));
        List<StoreOrderLogisticsDto> storeOrderLogisticsDtos
                = storeOrderLogisticsV1InnerServiceSMOImpl.queryStoreOrderLogisticss(storeOrderLogisticsDto);
        if (ListUtil.isNull(storeOrderLogisticsDtos)) {
            throw new CmdException("没有物流信息");
        }

        List<OrderExpressDto> orderExpressDtos =
                expressV1InnerServiceSMOImpl.getOrderExpress(storeOrderLogisticsDtos.get(0));

        context.setResponseEntity(ResultVo.createResponseEntity(orderExpressDtos));


    }
}
